<?php
/**
 *	管理用户和角色对应关系类
 *
 */
class User_Role_Role {

	const _USER_USER_ROLE	= 'user_user_role'; 
	const _USER_BASIC		= 'user_basic';
	
	/**
	 * 通过角色名获得用户信息uid
	 *
	 * @param string $role_name = 'manager'
	 *
	 * @return array 
	 */
	public static function get_user_role_by_name($role_name)
	{
		$role_name	= strval($role_name);

		if(empty($role_name))
			return false;

		$role_id	= User_Role_RoleDefine::get_role_id($role_name);
		$rs			= DB::select('uid')
						->from(self::_USER_USER_ROLE)
						->where('role','=',$role_id)
						->execute();
		return	$rs->as_array();
	}
	/**
	 *	通过用户id 获得角色名
	 *	
	 *	@param int $uid
	 *
	 *	@return string role_name
	 */
	public static function get_user_role($uid)
	{
		$uid	= (int) $uid;

		if($uid <=0 )
			return false;

		$rs		= DB::select('role')
					->from(self::_USER_USER_ROLE)
					->where('uid','=',$uid)
					->execute()
					->get('role', 0);
		
		return User_Role_RoleDefine::get_role_name($rs); 
	}

    /**
     *
     * 通过角色ID获得所有用户UID
     *
     * @param   string  $role_name
     * @return  array   $uids
     */
    public static function uids_by_role ($role_name)
    {
        $uids = array();

        //
        // get role id
        //
        $role_id = User_Role_RoleDefine::get_role_id($role_name);
        if ($role_id <= 0)
            return $uids;

        //
        // get uids
        //
        $query = DB::select('uid')
            ->from(self::_USER_USER_ROLE)
            ->where('role', '=', (int) $role_id)
            ->execute();

        if ($query->valid())
        {
            foreach ($query as $rs)
            {
                $uid = $rs['uid'];
                if ($uid > 0)
                    $uids[] = $uid;
            }
        }

        return $uids;
    }

	/**
	 *	通过用户id 获得角色所有信息
	 *	
	 *	@param int $uid
	 *
	 *	@return array
	 */
	public static function get_user_role_info($uid)
	{
		if(intval($uid) <= 0)
			return array();

		$rs		= DB::select()
					->from(self::_USER_USER_ROLE)
					->where('uid','=',$uid)
					->execute();
		
		$rs		= $rs->as_array();
		if(!empty($rs))
		{
			$role_id	= $rs[0]['role'];
			$data		= User_Role_RoleDefine::get_roleinfo_by_id($role_id);
			foreach($data[0] as $key => $val)
				$rs[0][$key]	= $val;
		}

		return empty($rs) ? array() : $rs;
	}

	/**
	 * 插入用户角色关系
	 *
	 * @param: int uid 
	 *		   int role_id
	 *
	 * @return boolean
	 *
	 */
	public function insert_user_role_by_id($uid,$role_id) 
	{
		$uid	= intval($uid);

		$role_id= intval($role_id);

		if($uid<=0 || $role_id <=0)
			return false;
		
		return	DB::insert(self::_USER_USER_ROLE)
					->columns(array('uid','role'))
					->values(array($uid,$role_id))
					->execute();
	}
	
    public function set_user_role_by_name ($uid, $role_name)
    {
        DB::delete(self::_USER_USER_ROLE)
            ->where('uid', '=', $uid)
            ->execute();

        return $this->insert_user_role_by_name($uid, $role_name);
    }

	/**
	 * 插入用户角色关系
	 *
	 * @param int uid ; string $role_name
	 *
	 * @return boolean
	 */
	public function insert_user_role_by_name($uid,$role_name) 
	{
		$uid		= intval($uid);

		$role_name	= strval($role_name);

		if($uid <= 0 || empty($role_name))
			return false;
		
		$role_id	= User_Role_RoleDefine::get_role_id($role_name);

		$rs			= DB::insert(self::_USER_USER_ROLE)
						->columns(array('uid','role'))
						->values(array($uid,$role_id))
						->execute();

		return $rs <= 0 ? false : true;

	}

	/**
	 * 更新用户角色关系
	 *
	 * @param  int uid ; string $role_name
	 *
	 * @return boolean
	 *
	 */
	public function update_user_role_by_uid($uid,$role_name) 
	{
		$uid		= intval($uid);
		
		$role_name	= strval($role_name);

		if($uid<=0 || empty($role_name))
			return false;
		
		$role_id	= User_Role_RoleDefine::get_role_id($role_name);

		return $this->update_user_role($uid, $role_id);
	}

	
	/**
	 *	通过UID	更新用户角色
	 *
	 *  @param int $uid, int $role_id
	 *
	 *	@return boolean
	 */
	public function update_user_role_by_roleid($uid,$role_id) 
	{
		$uid		= intval($uid);
		
		$role_id	= intval($role_id);

		if($uid<=0 || $role_id<=0)
			return false;
		
		return $this->update_user_role($uid, $role_id);
	}

	/**
	 * 根据UID删除用户角色关系 
	 *
	 * @param int $uid 
	 *
	 * @return boolean
	 */
	public function delete_user_role_by_uid($uid) 
	{
		$uid		= intval($uid);

		if($uid <= 0)
			return false;

		return $this->delete_user_role_by_uids(array($uid));
	}
	
	/**
	 * 根据UID批量删除用户角色关系 
	 *
	 * @param array $uids : array('2','3',...)
	 *
	 * @return boolean
	 */
	public function delete_user_role_by_uids(array $uids) 
	{
		if(! is_array($uids) || empty($uids))
			return false;

		$rs			= DB::delete(self::_USER_USER_ROLE)
						->where('uid','in',$uids)
						->execute();

		$role_data	= DB::select('utype')
						->from(self::_USER_BASIC)
						->where('uid', 'in', $uids)
						->execute()
						->as_array();

		for($i=0; $i< count($role_data); $i++)
		{
			if($role_data[$i]['utype'] != 'manager')
				return false;
		}

		$data		= DB::delete(self::_USER_BASIC)
						->where('uid','in',$uids)
						->execute();
		
		return $data <= 0 ? false : true;
	}

	private function update_user_role($uid, $role_id)
	{
		$uid	= intval($uid);

		$role_id= intval($role_id);

		$rs		= DB::update(self::_USER_USER_ROLE)
					->set(array('role' => $role_id))
					->where('uid','=',$uid)
					->execute();
	
		return $rs <= 0 ? false : true;
	}

}
?>
